<template>
  <div class="container">
    
  </div>
</template>

<script>
  import { getMenuList, apiPointLogin, projectList } from '@/api'
  import { getLocation } from '@/common/js/utils.js';

  export default {
    name: 'pcIndex',
    mounted() {
      let systemType = getLocation();

      if (systemType == 'operationManagement') {
        this.selectList()
      }
      
      this.getMenuList()
    },
    methods: {
      async selectList() {
        const res = await projectList({});

        if (res.rows.length > 0) {
          const projectKey = localStorage.getItem('projectKey');

          if (!projectKey && res.rows && res.rows.length) {
            localStorage.setItem('projectKey', res.rows[0].projectKey);
          }
        }
      },
      async getMenuList () {
        if (this.$route.query.source == "cloud") {
          localStorage.clear();
          let lock = await this.doPointLogin();
          if (!lock) return
        }

        if (!localStorage.getItem('token')) {
          location.replace('/login/')
          return
        }

        let menuList = []

        let res = await getMenuList({
          isSso: false,
          ifInMore:0,
          typeList: ['normal', 'redirect'],
          parentId: process.env.VUE_APP_ROOT_MENU
        })

        // return
        
        if (res.rows.length > 0) {
          menuList = res.rows
          localStorage.setItem('APP_MENU_DATA', JSON.stringify(menuList))

          this.changeMenu(menuList[0])
        } else {
          window.location.href = '/pcError?name=not-has-role'
        }
      },
      async changeMenu(item) {
        let separator = item.menuUrl.indexOf('?') > -1 ? '&' : '?'
        let menuUrl = this.changeDevHost(item.menuUrl)
        // console.log(menuUrl, 'menuUrl')
        // return
        let token = item.token || localStorage.getItem('token')
        window.location.replace(menuUrl + separator + 'token=' + token + '&menuId=' + item.primaryKey)
      },
      changeDevHost (url) {
        let urlHost = url.match(/^(?:[^\/]|\/\/)*/)
        // 开发环境替换域名为本地ip
        if (process.env.NODE_ENV === 'development' && window.devHostMap && urlHost && window.devHostMap[urlHost]) {
          url = url.replace(urlHost, window.devHostMap[urlHost])
        }
        return url
      },
      //单点
      async doPointLogin(){
        let res = await apiPointLogin(this.$route.query.ticket)
        localStorage.setItem("userInfo",JSON.stringify(res));
        localStorage.setItem("token",res.extend.token);
        localStorage.setItem('publicKey', JSON.stringify(res.extend.publicKey));
        localStorage.setItem('roleCode', res.extend.roleCode ? JSON.stringify(res.extend.roleCode) : '');
        localStorage.setItem('authorities', res.extend.menuList ? JSON.stringify(res.extend.menuList) : '');
        localStorage.setItem('authorityCode', res.extend.roleCode ? JSON.stringify(res.extend.roleCode) : '');
        localStorage.removeItem('APP_MENU_DATA')
        return res.extend.token ? true : false;
      }
    },
  };
</script>

<style scoped lang="scss">
  .container {
    width: 100vw;
    height: 100vh;
    background-color: #fff;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    padding-top: 15vh;
    box-sizing: border-box;

    .info {
      margin-top: 10vh;
      font-size: 20px;
    }
  }
</style>
